package com.dk.service.item;

import com.alibaba.dubbo.config.annotation.Service;
import com.dk.ItemCatResult;
import com.dk.mapper.item.TbItemCatMapper;
import com.dk.pojo.item.TbItemCat;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Component
public class ItemCatServiceImpl implements ItemCatService {

    @Resource
    private TbItemCatMapper catMapper;


    @Override
    public int deleteItemCatByIds(Long[] ids) {
        return catMapper.deleteItemCatByIds(ids);
    }

    @Override
    public int updateItemCat(TbItemCat itemCat) {
        return catMapper.updateByPrimaryKeySelective(itemCat);
    }

    @Override
    public TbItemCat itemCatById(Long id) {
        return catMapper.selectByPrimaryKey(id);
    }

    @Override
    public int saveItemCat(TbItemCat itemCat) {
        return catMapper.insertSelective(itemCat);
    }

    @Override
    public Map<String, Object> listItemCatByPid(Long parentId) {
        // new 一个map容器
        Map<String,Object> map = new HashMap<>();
        List<TbItemCat> itemCats = catMapper.listItemCatByPid(parentId);
        List<ItemCatResult> list = new ArrayList<>();
        List<ItemCatResult> resultList = listItemCatResult(list,parentId);
        map.put("itemCatList",itemCats);
        map.put("resultList",resultList);
        return map;
    }

    private List<ItemCatResult> listItemCatResult(List<ItemCatResult> resultList ,Long parentId) {
        if (parentId == 0) {
            resultList.add(new ItemCatResult((long) 0,"顶级分类列表"));
        } else {
            TbItemCat tbItemCat = catMapper.selectByPrimaryKey(parentId);
            resultList.add(new ItemCatResult(tbItemCat.getId(),tbItemCat.getName()));
            listItemCatResult(resultList,tbItemCat.getParentId());
        }
        return resultList;
    }
}
